//
//  ViewController.m
//  HDQRcodeDemo
//
//  Created by HDHaoShaoPeng on 2017/5/10.
//  Copyright © 2017年 HDHaoShaoPeng. All rights reserved.
//

#import "ViewController.h"
#import "HD_HSP_QRCodeView.h"

@interface ViewController ()<HD_HSP_QRCodeViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *codeView;
@property (weak, nonatomic) IBOutlet UIImageView *QRCodeImageView;
@property (weak, nonatomic) IBOutlet UITextView *myTF;


@property(strong,nonatomic)HD_HSP_QRCodeView *QRView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}


-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    HD_HSP_QRCodeView *view = [[HD_HSP_QRCodeView alloc]initWithFrame:self.codeView.bounds];
    view.myDelegate = self;
    [self.codeView addSubview:view];
    
    [view startRunning];
    
    self.QRCodeImageView.image = [view giveMeQRCodeImageWithString:@"http://www.cnblogs.com/davidyff/p/5254578.html" imageSize:200 andLogoImage:[UIImage imageNamed:@"80"]];
    
//    if (!_QRView) {
//        [self.codeView addSubview:self.QRView];
//        [self.QRView startRunning];
//    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(HD_HSP_QRCodeView *)QRView
{
    if (!_QRView) {
        _QRView = [[HD_HSP_QRCodeView alloc]initWithFrame:self.codeView.bounds];
        _QRView.myDelegate = self;
    }
    return _QRView;
}

-(void)giveBackSearchStringWith:(NSString *)string
{
    self.myTF.text = string;

//    dispatch_async(dispatch_get_main_queue(), ^{
//    })
}

-(void)pleaseOpenCameraPermission
{
    [self showAlertViewMessage:@"请打开相机权限"];
}

-(void)showAlertViewMessage:(NSString *)message
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        if([[UIApplication sharedApplication] canOpenURL:url]) {
            [[UIApplication sharedApplication] openURL:url];
        }
        
    }];
    
    
    [alert addAction:action1];
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];
}

@end
